﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MiniCPU.Entities
{
    /// <summary>
    /// Data Propagator class.
    /// </summary>
    public class DataPropagator : LimitedData
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="DataPropagator"/> class.
        /// </summary>
        /// <param name="name">The name of the data propagator.</param>
        /// <param name="value">The value of the data propagator.</param>
        /// <param name="numberOfBits">The number of bits.</param>
        public DataPropagator(string name, Int32 value, int numberOfBits)
            : base(name, value, numberOfBits)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataPropagator"/> class.
        /// </summary>
        /// <param name="name">The name of the data propagator.</param>
        /// <param name="value">The value of the data propagator.</param>
        /// <param name="valueBase">The value base.</param>
        /// <param name="numberOfBits">The number of bits.</param>
        public DataPropagator(string name, string value, int valueBase, int numberOfBits) :
            this(name, Convert.ToInt32(value, valueBase), numberOfBits)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataPropagator"/> class.
        /// </summary>
        /// <param name="name">The name of the data propagator.</param>
        /// <param name="value">The value of the data propagator.</param>
        /// <param name="numberOfBits">The number of bits.</param>
        public DataPropagator(string name, byte value, int numberOfBits) :
            this(name, Convert.ToInt32(value), numberOfBits)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataPropagator"/> class.
        /// </summary>
        /// <param name="name">The name of the data propagator.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public DataPropagator(string name, bool value)
            : this(name, Convert.ToInt32(value), 1)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataPropagator"/> class.
        /// </summary>
        /// <param name="name">The name of the data propagator.</param>
        /// <param name="value">The value of the data propagator.</param>
        /// <param name="numberOfBits">The number of bits.</param>
        public DataPropagator(string name, byte[] value, int numberOfBits) :
            this(name, GetIntFromByteArray(value), numberOfBits)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataPropagator"/> class.
        /// </summary>
        /// <param name="data">The data.</param>
        public DataPropagator(string name, LimitedData data, int startBit, int endBit):
            base(name, data, startBit, endBit)
        {
        }
        #endregion

        #region Methods

        /// <summary>
        /// Updates the value.
        /// </summary>
        /// <param name="value">The value.</param>
        protected override void UpdateValue(int value)
        {
            base.UpdateValue(value);
            ExecuteEventHandlers();
            IsDataChanged = false;
        }
        #endregion
    }
}
